package comqyf.nacoscloudzookeeper.config;


import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
public class ZooConfig {
    private CuratorFramework client;
    public static final String HOST = "148.70.142.97:2181";

    public ZooConfig() {
        ExponentialBackoffRetry retry = new ExponentialBackoffRetry(1000, 3);
        client = CuratorFrameworkFactory.builder().connectString(HOST)
                .connectionTimeoutMs(5000)
                .sessionTimeoutMs(3000)
                .retryPolicy(retry).build();
    }

    public void connect(){
        client.start();
        try {
            boolean connected = client.blockUntilConnected(5000, TimeUnit.SECONDS);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Bean
    public CuratorFramework getClient() {
        connect();
        return client;
    }


}
